package com.example.universitylibraryback.demo;

import com.example.universitylibraryback.pipeline.Pipeline;
import com.example.universitylibraryback.pipeline.BorrowContext;
import com.example.universitylibraryback.pipeline.filter.BorrowRequestParserFilter;
import com.example.universitylibraryback.pipeline.filter.ValidationFilter;
import com.example.universitylibraryback.pipeline.filter.ReaderQualificationFilter;
import com.example.universitylibraryback.pipeline.filter.BookStatusFilter;
import com.example.universitylibraryback.pipeline.filter.BorrowRecordGenerateFilter;
import com.example.universitylibraryback.pipeline.filter.BookStatusUpdateFilter;

import java.util.concurrent.TimeUnit;

/**
 * 图书借阅流程演示类
 * 演示管道-过滤器架构在图书借阅过程中的应用
 */
public class BorrowProcessDemo {
    public static void main(String[] args) {
        try {
            // 1. 创建管道并组装过滤器（顺序很重要！）
            Pipeline<BorrowContext> pipeline = new Pipeline<>();
            pipeline.addFilter(new BorrowRequestParserFilter())
                    .addFilter(new ValidationFilter())
                    .addFilter(new ReaderQualificationFilter())
                    .addFilter(new BookStatusFilter())
                    .addFilter(new BorrowRecordGenerateFilter())
                    .addFilter(new BookStatusUpdateFilter());

            // 2. 模拟不同的输入数据
            String testInput1 = "R001,B001"; // 有效输入：有效读者+有效可借图书
            String testInput2 = "R002,B002"; // 无效输入：有效读者+已借出图书
            String testInput3 = "R006,B003"; // 无效输入：无效读者+有效图书
            String testInput4 = "R004,B004"; // 无效输入：有逾期记录的读者
            String testInput5 = "invalid_format"; // 无效输入：格式错误

            // 3. 创建上下文对象并提交到管道处理
            System.out.println("====== 开始处理借阅请求 ======");
            System.out.println("提交请求1: " + testInput1);
            pipeline.submit(new BorrowContext(testInput1));
            
            System.out.println("提交请求2: " + testInput2);
            pipeline.submit(new BorrowContext(testInput2));
            
            System.out.println("提交请求3: " + testInput3);
            pipeline.submit(new BorrowContext(testInput3));
            
            System.out.println("提交请求4: " + testInput4);
            pipeline.submit(new BorrowContext(testInput4));
            
            System.out.println("提交请求5: " + testInput5);
            pipeline.submit(new BorrowContext(testInput5));

            // 等待处理完成
            System.out.println("====== 请求已提交，等待处理完成 ======");
            TimeUnit.SECONDS.sleep(3); // 给足够时间让所有过滤器处理完请求

            // 4. 关闭管道
            pipeline.shutdown();
            System.out.println("====== 借阅流程演示结束 ======");

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("演示过程被中断: " + e.getMessage());
        }
    }
}